/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.bean.teacher;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import javax.faces.context.FacesContext;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ReverseComparator;
import org.apache.log4j.Logger;
import vn.com.pps.entities.Teacher;
import vn.com.pps.web.common.component.table.LazyCacheModel;
import vn.com.pps.web.ui.TeacherTableItem;

/**
 *
 * @author Dai
 */
public class TeacherManageBean extends LazyCacheModel implements Serializable {

    private Logger logger = Logger.getLogger(TeacherManageBean.class);
    private String firstName = "";
    private String lastName = "";
    private String mobile = "";
    private ArrayList<TeacherTableItem> teacherTableItems;
    private int rowCount = 0;
    private String WhereClause = "1=1";
    private int selectedId;
    private Teacher teacher;
    private int indexTab = 1;
    private boolean isView = true;

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public int getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public ArrayList<TeacherTableItem> getTeachers() {
        return teacherTableItems;

    }

    @Override
    protected void init() {
        rowCount = getServiceLocator().getTeacherService().countTeacher("1=1");
        setTargetPage(1);
        setRowCount(rowCount);
        teacherTableItems = getServiceLocator().getTeacherService().getTeachers(getTargetPage(), getMaximumPageSize(), " 1=1");
        setPageItems(teacherTableItems);
    }

    @Override
    public void sort(String pm_column, boolean pm_ascending) {
        Comparator<TeacherTableItem> bc = null;
        if (pm_ascending) {
            bc = new BeanComparator(pm_column);
        } else {
            bc = new BeanComparator(pm_column, new ReverseComparator(new ComparableComparator()));
        }
        Collections.sort(getPageItems(), bc);
    }

    @Override
    public void loadData() {
        rowCount = getServiceLocator().getTeacherService().countTeacher("1=1");
        setTargetPage(1);
        setRowCount(rowCount);
        teacherTableItems = getServiceLocator().getTeacherService().getTeachers(getTargetPage(), getMaximumPageSize(), " 1=1");
        setPageItems(teacherTableItems);
    }

    public void search() {
        WhereClause = "1=1 ";
        if (firstName.trim().length() > 0) {
            WhereClause += " AND FIRSTNAME LIKE '%" + firstName.trim() + "%'";
        }

        if (lastName.trim().length() > 0) {
            WhereClause += " AND LASTNAME LIKE '%" + lastName.trim() + "%'";
        }

        if (mobile.trim().length() > 0) {
            WhereClause += " AND MOBILE LIKE '%" + mobile.trim() + "%'";
        }

        System.out.println("Where::" + WhereClause);
        rowCount = getServiceLocator().getTeacherService().countTeacher(WhereClause);
        setTargetPage(1);
        teacherTableItems = getServiceLocator().getTeacherService().getTeachers(getTargetPage(), getMaximumPageSize(), WhereClause);
        setPageItems(teacherTableItems);
        setRowCount(rowCount);
    }

    public void resetForm() {
        firstName = "";
        lastName = "";
        mobile = "";
    }

    public String viewTeacher() {
        System.out.println("viewTeacher ID:" + selectedId);
        indexTab = 1;
        isView = true;
        teacher = getServiceLocator().getTeacherService().getTeacher(selectedId);
        System.out.println("TTEST:"+teacher.getFirstName());
        FacesContext facesContext = FacesContext.getCurrentInstance();
        CourseTeacherBean courseTeacherBean
                = (CourseTeacherBean) facesContext.getApplication()
                .createValueBinding("#{courseTeacherBean}").getValue(facesContext);
        courseTeacherBean.setTeacherID(selectedId);
        courseTeacherBean.loadData();
        return "success";

    }

    public String preUpdateTeacher() {
        isView = false;
        indexTab = 1;
        teacher = getServiceLocator().getTeacherService().getTeacher(selectedId);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Teacher courseTeacherBean
                = (Teacher) facesContext.getApplication()
                .createValueBinding("#{teacherEntity}").getValue(facesContext);

        courseTeacherBean.setFirstName(teacher.getFirstName());
        courseTeacherBean.setLastName(teacher.getLastName());
        courseTeacherBean.setEmail(teacher.getEmail());
        courseTeacherBean.setMobile(teacher.getMobile());
        courseTeacherBean.setNote(teacher.getNote());
        courseTeacherBean.setId(teacher.getId());
        return "success";

    }

    public String updateTeacher() {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        Teacher courseTeacherBean
                = (Teacher) facesContext.getApplication()
                .createValueBinding("#{teacherEntity}").getValue(facesContext);
        Calendar calendar = Calendar.getInstance();
        java.sql.Timestamp currentTimeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
        courseTeacherBean.setLastModifiedDate(currentTimeStamp);
        getServiceLocator().getTeacherService().editTeacher(courseTeacherBean);
        loadData();
        return "success";

    }

    public String addTeacher() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Teacher teacherEntity
                = (Teacher) facesContext.getApplication()
                .createValueBinding("#{teacheEntity}").getValue(facesContext);
        System.out.println("Teacher::" + teacherEntity.getLastName());
        Calendar calendar = Calendar.getInstance();
        java.sql.Timestamp currentTimeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
        teacherEntity.setCreatedDate(currentTimeStamp);
        teacherEntity.setLastModifiedDate(currentTimeStamp);
        getServiceLocator().getTeacherService().editTeacher(teacherEntity);
        loadData();
        return "success";
    }
    public String preaddTeacher(){
        return "success";
    }
    public int getIndexTab() {
        return indexTab;
    }

    public void setIndexTab(int indexTab) {
        System.out.println("getIndexTab::" + indexTab);
        this.indexTab = indexTab;
    }

    public boolean isIsView() {
        return isView;
    }

}
